package mysql

import (
	"fmt"
	"goods-srv/appconfig"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"sync"
)

var (
	mysqlClient *gorm.DB
	err         error
	once        sync.Once
)

func InitMysql() {
	user := appconfig.AppConf1.MysqlConfig.User
	host := appconfig.AppConf1.MysqlConfig.Host
	password := appconfig.AppConf1.MysqlConfig.Password
	name := appconfig.AppConf1.MysqlConfig.DBName
	port := appconfig.AppConf1.MysqlConfig.Port
	dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=Local", user, password, host, port, name)
	once.Do(func() {
		mysqlClient, err = gorm.Open(mysql.Open(dsn), &gorm.Config{})
		if err != nil {
			fmt.Println("mysql连接失败")
			return
		}
		mysqlClient.AutoMigrate(new(User), new(Goods))
	})
}
